<template>
  <div class="container">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="margin-top: 10px">
      <!-- 搜索框 -->
      <el-row>
        <el-input
          clearable
          v-model="params.query"
          style="width: 30%"
          placeholder="请输入定单号"
          @clear="this.getOrders()"
        >
          <template #append>
            <el-button icon="el-icon-search" @click="onSearch"></el-button>
          </template>
        </el-input>
      </el-row>
      <el-table :data="list" border style="margin-top: 20px">
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column
          label="订单编号"
          prop="order_number"
          width="280px"
        ></el-table-column>
        <el-table-column label="订单价格" prop="order_price"></el-table-column>
        <el-table-column label="是否支付" prop="pay_status">
          <template #default="{ row }">
            <el-tag v-if="row.pay_status === '0'" type="danger">未支付</el-tag>
            <el-tag v-else type="success">已支付</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send"></el-table-column>
        <el-table-column label="下单时间" prop="create_time" width="280px">
          <template #default="{ row }">
            {{ row.create_time | formatTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button
              type="primary"
              circle=""
              icon="el-icon-edit"
              size="small"
              @click="edit(row.order_id)"
            ></el-button>
            <el-button
              type="success"
              circle=""
              icon="el-icon-location"
              size="small"
              @click="look(row.order_number)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row style="margin-top: 10px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="params.total"
          :current-page="params.pagenum"
          :page-size="params.pagesize"
          :page-sizes="[5, 10, 15, 20]"
        >
        </el-pagination>
      </el-row>
    </el-card>
    <!-- 物流进度弹出层 -->
    <OrderTimeline :showTimeline.sync="showTimeline" ref="OrderTimeline" />
    <!-- 修改地址弹出层 -->
    <EditAdress :showEdit.sync="showEdit" :editId='editId' ref="EditAdress" />
  </div>
</template>

<script>
import { orders } from '@/api/orders'
import OrderTimeline from './orderTimeline.vue'
import EditAdress from './editAdress.vue'
export default {
  components: { OrderTimeline, EditAdress },
  data () {
    return {
      params: {
        query: '',
        total: 0,
        pagenum: 1,
        pagesize: 10
      },
      list: [], // 订单列表
      showTimeline: false, // 显示时间轴
      showEdit: false, // 显示编辑弹窗
      editId: ''
    }
  },
  created () {
    this.getOrders()
  },
  methods: {
    handleSizeChange (newSize) {
      this.params.pagesize = newSize
      this.getOrders()
    },
    handleCurrentChange (newPage) {
      this.params.pagenum = newPage
      this.getOrders()
    },
    async getOrders () {
      const res = await orders(this.params)
      this.params.total = res.total
      this.list = res.goods
    },
    async onSearch () {
      this.params.pagenum = 1
      if (this.params.query.trim() === '') {
        return this.$message.warning('请输入关键字')
      }
      const { goods } = await orders({ pagenum: 1, pagesize: 100 })
      const res = goods.filter(
        (item) => item.order_number.indexOf(this.params.query) !== -1
      )
      this.list = res
    },
    look (id) {
      this.$refs.OrderTimeline.getLogistics(id)
      this.showTimeline = true
    },
    edit (id) {
      this.editId = id
      this.showEdit = true
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  padding: 20px;
  background-color: #f5f7f8;
}
</style>
